package com.team2.outpatient_sys.service.impl;

import com.team2.outpatient_sys.dao.OutpatientScheduleDao;
import com.team2.outpatient_sys.pojo.DeptFilter;
import com.team2.outpatient_sys.pojo.DoctorAndNum;
import com.team2.outpatient_sys.pojo.ScheduleAndDoctor;
import com.team2.outpatient_sys.service.OutpatientScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Service
@Transactional
public class OutpatientScheduleServiceImpl implements OutpatientScheduleService {
    @Autowired
    private OutpatientScheduleDao outpatientScheduleDao;

    /**
     * 查询所有排班方法的实现
     * @return 排序后的所有排班的list
     */
    @Override
    public List<ScheduleAndDoctor> findAllSchedule() {
        return outpatientScheduleDao.findAllSchedule();
    }

    /**
     * 按医生姓名查询排班方法的实现
     * @param name 医生姓名
     * @return 排序后的相应排班的list
     */
    @Override
    public List<ScheduleAndDoctor> findScheduleByDoctorName(String name) {
        //不管传来的参数前后有什么字符
        name = "%" + name + "%";
        return outpatientScheduleDao.findScheduleByDoctorName(name);
    }

    /**
     * 单选或批量修改排班医生方法的实现
     * @param did 医生id
     * @param sIds 排班id
     * @return 修改总数与失败数的map
     */
    @Override
    public Map<String, Integer> updateSchedule(Integer did, List<Integer> sIds) {
        Map<String, Integer> map = new TreeMap<>();
        Integer i = 0;
        //存储总数
        map.put("total",sIds.size());

        for (Integer sId : sIds) {
            //修改医生排班关系表，当修改失败，失败数+1
            if(outpatientScheduleDao.updateDoctorSchedule(did,sId) < 1) {
                i++;
            }
        }
        //存储失败数
        map.put("fail",i);

        return map;
    }

    /**
     * 按排班升序查询所有医生方法的实现
     * @return 排序后的所有医生对象的list
     */
    @Override
    public List<DoctorAndNum> findAllDoctorAsc() {
        return outpatientScheduleDao.findAllDoctorAsc();
    }

    /**
     * 查询所有科室筛选器方法的实现
     * @return 所有科室筛选器的list
     */
    @Override
    public List<DeptFilter> findAllDeptFilter() {
        return outpatientScheduleDao.findAllDeptFilter();
    }
}
